package com.crr.demo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.crr.demo.bean.User;
import com.crr.demo.common.ResultModel;
import com.crr.demo.impl.service.UserService;
import com.crr.demo.utils.DateUtils;
import com.crr.demo.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import static org.springframework.web.bind.annotation.RequestMethod.*;

@RestController
public class UserController {
    @Resource
    UserService userService;
    @Autowired
    HttpServletRequest request;
    /**
     * 获取用户列表
     * @return
     */
    @RequestMapping(value = "/userlist" ,method = GET)
    public ResultModel GetAllUser() {
        String token = request.getHeader("Authorization");
        User user = userService.getUser(JWTUtils.verify(token));
        if(user==null){
            return new ResultModel(105,DateUtils.getTime());
        }
        List<JSONObject> userList = userService.getAllUser();
        if(userList==null)return new ResultModel(106,DateUtils.getTime());
        return new ResultModel(100,DateUtils.getTime(),userList);
    }

    /**
     * 用户删除
     * @param user
     * @return
     */

    @RequestMapping(value = "/userdelete" ,method = DELETE)
    public ResultModel DeleteUser(@RequestBody User user){
        String token = request.getHeader("Authorization");
        User adminUser = userService.getUser(JWTUtils.verify(token));
        if(adminUser==null){
            return new ResultModel(105,DateUtils.getTime());
        }
        Long roles=adminUser.getRole();
        if(roles.longValue()>=9) {
            user.setUpdateTime(DateUtils.getTime());
            user.setDeleteTime(DateUtils.getTime());
            String result = userService.deleteUser(user);
            return new ResultModel(result,100, DateUtils.getTime());
        }
        return new ResultModel(101, DateUtils.getTime());
    }

    /**
     * 用户重激活
     */
    @RequestMapping(value = "/userdelete" ,method = PUT)
    public ResultModel RecoverUser(@RequestBody User user){
        String token = request.getHeader("Authorization");
        User adminUser = userService.getUser(JWTUtils.verify(token));
        if(adminUser==null){
            return new ResultModel(105,DateUtils.getTime());
        }
        Long roles=adminUser.getRole();
        if(roles.longValue()>=9) {
            user.setUpdateTime(DateUtils.getTime());
            user.setDeleteTime(null);
            String result = userService.recoverUser(user);
            return new ResultModel(result,100, DateUtils.getTime());
        }
        return new ResultModel(101, DateUtils.getTime());
    }


    /**
     * 用户权限提升
     * @param authUser
     * @return
     */

    @RequestMapping(value="/auth",method = PATCH)
    public ResultModel AuthUser(@RequestBody User authUser) {
        String token = request.getHeader("Authorization");
        User user = userService.getUser(JWTUtils.verify(token));
        Long roles=user.getRole();
        if(roles.longValue()>=7){
            Long newId = authUser.getId();
            Long newRole = authUser.getRole();
            userService.authUser(newId, newRole);
            authUser=userService.getUserById(newId);
            JSONObject jsonObject  = new JSONObject(16,true);
            JSON.toJSONStringZ(jsonObject, SerializeConfig.getGlobalInstance(), SerializerFeature.QuoteFieldNames);
            jsonObject.put("id",authUser.getId());
            jsonObject.put("name",authUser.getName());
            jsonObject.put("role",authUser.getRole());
            return new ResultModel(jsonObject,100,DateUtils.getTime());
        }
        return new ResultModel(101,DateUtils.getTime());
    }

    /**
     * 用户新增
     * @param request
     * @return
     */

    @RequestMapping(value="/user",method = POST)
    public ResultModel AddUser(@RequestBody User user, HttpServletRequest request){
        String name = user.getName();
        String identityId = user.getIdentityId();
        String token = request.getHeader("Authorization");
        if(userService.getUser(identityId)!=null){//查询数据库是否存在改学号，如果存在则创建失败
            return new ResultModel(107,DateUtils.getTime());
        }
        User adminUser = userService.getUser(JWTUtils.verify(token));
        Long role=adminUser.getRole();
        if(role.longValue()>=7){
            user.setPwd(identityId);
            user.setCreateTime(DateUtils.getTime());
            user.setUpdateTime(DateUtils.getTime());
            userService.addUser(user);
            JSONObject jsonObject  = new JSONObject(16, true);
            JSON.toJSONStringZ(jsonObject, SerializeConfig.getGlobalInstance(), SerializerFeature.QuoteFieldNames);
            jsonObject.put("id",userService.getUser(identityId).getId());
            jsonObject.put("name",name);
            jsonObject.put("identityId",identityId);
            return new ResultModel(jsonObject,100, DateUtils.getTime());
        }
        return new ResultModel(101,DateUtils.getTime());
    }

    /**
     * 用户信息修改
     */
    @RequestMapping(value="/userinfo",method = PUT)
    public ResultModel UpdateUser(@RequestBody User user){
        String token = request.getHeader("Authorization");
        User adminUser = userService.getUser(JWTUtils.verify(token));
        if(adminUser==null){
            return new ResultModel(105,DateUtils.getTime());
        }
        Long id=user.getId();
        Long role=adminUser.getRole();
        User pastUser=userService.getUserById(id);
        if(role.longValue()<7){
            return new ResultModel(101, DateUtils.getTime());
        }else{
            user.setUpdateTime(DateUtils.getTime());
            if(user.getName()!=null)pastUser.setName(user.getName());
            if(user.getPwd()!=null) pastUser.setPwd(user.getPwd());
            if(user.getIdentityId()!=null)pastUser.setIdentityId(user.getIdentityId());
            if(user.getActive()!=null){
                if(role.longValue()>=9){
                    pastUser.setActive(user.getActive());
                }else{
                    return new ResultModel(101, DateUtils.getTime());
                }
            }
        }
        userService.updateUser(pastUser);
        return new ResultModel(100, DateUtils.getTime());
    }
}
